import Ember from 'ember';

export default Ember.Service.extend({
  pageTitles: {},
  roles: [],
  rolesnv: Ember.computed("roles", function(){
    var roles = this.get('roles');
    return roles.map((it) => {
      return {n: it.get('name'), v: it.get('id')};
    });
  }),
  lastEditModel: {},
  findRoleByName: function(rn)  {
    this.get('roles').find(function(it) {
      return it.get('name') === rn;
    });
  },
  apiPrefix: "/api/v1",
  toggleWorking(start) {
    if (start) {
      Ember.$('body').addClass("working");
    } else {
      Ember.$('body').removeClass("working");
    }
  },
  findRoleById: function(rid) {
    var srid = rid + "",
      r =  this.get('roles').find(function(it) {
      // console.log(it.get('id') + ":" + rid);
      // console.log(typeof rid);
      // console.log(typeof it.get('id'));
        return (it.get('id') === srid);
      });
    return r;
  },
  queryToQ: function(query) {
    if (query) {
      var q = "";
      for(var k in query) {
        q = k + '=' + query[k] + ',';
      }
      if (q) {
        return {q: q};
      } else {
        return {};
      }
    } else {
      return {};
    }
  },
  groupTree: null,
});
